<template>
  <div class='headtop'>
    <div class="hdLeft">
      <div class='logo'>
        <router-link to='/'>
          <div class="flex-box">
            <p>CGClass</p>
            <div class="version">&nbsp;V{{ versionRef }}</div>
          </div>
        </router-link>
      </div>

    </div>

    <div class='user'>
      <el-dropdown trigger='click' @command='handleCommand'>
        <div class='row'>
          <div class='pic'>
            <img v-if="store.avatar" :src='store.avatar' alt=''>
            <img v-else src='@/assets/images/user-default.jpg' alt=''>
          </div>
          <div class='nickname'>
            {{store.userName }}
          </div>
          <div class='icon'>
            <span class='iconfont icon-xiangxia1'></span>
          </div>
        </div>
        <template #dropdown>
          <el-dropdown-menu >
          
            <div @click="goPersonal">
                <el-dropdown-item><el-icon>
                    <Edit />
                  </el-icon>个人中心</el-dropdown-item>
                </div>
         
                <p style="font-size:12px; line-height:1.7em; color:#999; text-align:center;">角色切换</p>
         

              <el-dropdown-item v-if="useUserStore().is_super" v-for="item in superAdmin" 
              :command="item" :disabled="store.rolesName == item">
                <el-icon>
                  <User />
                </el-icon>{{rolesName(item)}}
              </el-dropdown-item>
    
              <el-dropdown-item v-else v-for="item in useUserStore().roles" :command="item.name" :disabled="store.rolesName == item.name">
                <el-icon>
                  <User />
                </el-icon>{{rolesName(item.name)}}
              </el-dropdown-item>
              <el-dropdown-item divided command="logout">
                <el-icon>
                  <SwitchButton />
                </el-icon> 退出登录
              </el-dropdown-item>

          </el-dropdown-menu>
        </template>
      </el-dropdown>
    </div>


  </div>
</template>

<script setup>
import { SwitchButton} from '@element-plus/icons-vue';
import { version } from '@/../package.json';
import {rolesName} from '@/utils/common'
import router from '@/router';
import {useUserStore} from "@/store/modules/user"
import { ElMessage } from 'element-plus'
import { dynamicRoutes } from "@/router"
import Layout from '@/layout'
const store=useUserStore()
const versionRef = ref(version)

function handleCommand(command) {
  switch (command) {
    case "setLayout":
      setLayout();
      break;
    case "logout":
      logout();
      break;
      case "老师组":
      isTeacher();
      break;
      case "管理组":
      isAdmin();
      break;
      case "学生组":
       isStudent();
      break;
    default:
      break;
  }
}


// 切换老师端
function isTeacher() {
  store.rolesName="老师组"
 
  router.addRoute({
          path: '/',
          component: Layout,
          redirect: '/index',
          children: dynamicRoutes(useUserStore().rolesName)
     })
      router.push('/index')
}
// 
//切换管理端

function isAdmin(){
  store.rolesName="管理组"

 router.addRoute({
          path: '/',
          component: Layout,
          redirect: '/index',
          children: dynamicRoutes('管理组')
        })
        router.push('/index')
}

//切换学生组
function isStudent(){
  
  store.rolesName="学生组"
 router.addRoute({
          path: '/',
          component: Layout,
          redirect: '/home',
          children: dynamicRoutes(useUserStore().rolesName)
        })

        router.push('/index')
}
// 退出登陆的方法
function logout() {
 
  ElMessageBox.confirm('确定注销并退出系统吗？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    localStorage.clear()
    sessionStorage.clear()
    router.go(0)
  }).catch(() => { });
}


// 跳转到个人中心

const goPersonal=()=>{
  router.push({path:"/user",query:{activeNum:1}})

}
</script>

<style lang="scss" scoped>
@use "@/assets/styles/vars.module" as *;

.roles {
  border-top: 1px solid #e4e4e4;
  margin-top: 5px;
  padding-top: 5px;
  .name {
    font-size: 12px;
    color: #999;
    text-align: center;
    padding-bottom: 3px;
  }
}
.headtop {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
  min-width: 0;
  height: 100%;
  padding: 0 30px;
  background: #fff;
  box-sizing: border-box;
  border-bottom: 1px solid #ddd;


  .hdLeft {
    display: flex;
    align-items: center;
    height: 100%;

    .nav {
      margin-left: 100px;
      height: 100%;
      min-width: 500px;

      ul {
        display: flex;
        height: 100%;

        li {
          a {
            display: flex;
            height: 100%;
            align-items: center;
            color: #333;
            padding: 0 20px;
            font-size: 14px;

            .iconfont {
              font-size: 16px;
              margin-right: 4px;
            }
          }

          a:hover {
            background: #E7F1FF;
            color: #4395FF;
          }
        }

        li.active {
          a {
            color: #4395FF;
          }
        }
      }
    }
  }

  .logo {
    .flex-box {
      display: flex;
      align-items: center;
      p{
        font-size: 20px;
        line-height: 1em;
      }
      

      .version {
        font-size: 12px;
        color: #aaa;
       padding-top: 5px;
       ;
      }
    }
  }



  .user {
    display: flex;
    align-items: center;

    .background-progress-wrap {
      display: flex;
      justify-content: center;
      align-items: center;
      width: 140px;
      height: 35px;
      margin-right: 15px;
    }

    .row {
      display: flex;
      align-items: center;
      cursor: pointer;

      .pic {
        width: 34px;
        height: 34px;
        border-radius: 50%;
        overflow: hidden;

        img {
          width: 100%;
          height: 100%;
        }
      }

      .nickname {
        font-size: 16px;
        padding: 0 10px;
      }

      .icon {
        .iconfont {
          font-size: 14px;
          color: #888;
        }
      }
    }
  }


}
</style>
